package com.newa.actions;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.newa.beans.Section;
import com.newa.dao.DAOSection;
import com.newa.listeners.Listener;
import com.newa.utils.HibUtil;
import com.opensymphony.xwork2.ActionSupport;

public class updateSection extends ActionSupport {
	private Logger LOG = Logger.getLogger(updateSection.class);
	private Section section;
	private int section_id;

	@Override
	public String execute() throws Exception {
		Session session = HibUtil.getSessionFactory().openSession();
		DAOSection daos = new DAOSection(session);
		try {
			Section sec = daos.getSectionById(section.getID());
			// section.setID(section_id);
			section.setWORKS(sec.getWORKS());
			session.clear();
			session.flush();
			daos.updateSection(section);
			setSection_id(section.getID());
		} catch (Exception e) {
			LOG.warn(e);
			throw new RuntimeException(e);
		} finally {
			daos.closeSession().close();
		}
		return "success";
	}

	public String prepare() {
		Session session = HibUtil.getSessionFactory().openSession();
		DAOSection daos = new DAOSection(session);
		try {
			setSection(daos.getSectionById(section_id));
		} catch (Exception e) {
			LOG.warn(e);
			throw new RuntimeException(e);
		} finally {
			daos.closeSession().close();
		}
		return "prepare";
	}

	public int getSection_id() {
		return section_id;
	}

	public void setSection_id(int section_id) {
		this.section_id = section_id;
	}

	public Section getSection() {
		return section;
	}

	public void setSection(Section section) {
		this.section = section;
	}

}
